<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="titleStr"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData"  :rules="rules">
                <el-form-item prop="goods_code">
                    <el-input v-model="formData.goods_code" :placeholder="`请输入${titleStr}`" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { orderRemark, orderdetail } from '@/api/order'
import Popup from '@/components/popup/index.vue'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()


const rules = {
    goods_code: [
        {
            required: true,
            message: "请输入商品编号",
            trigger: ["blur", "change"],
        },
    ],
};

const titleStr = ref('');

const formData = reactive({
    goods_code: '',
    str: '',
    type: '',
});

const open = () => {
    popupRef.value?.open()
};

const setFormData = (data: Record<any, any>) => {
    // for (const key in formData) {
    //     if (data[key] != null && data[key] != undefined) {
    //         //@ts-ignore
    //         formData[key] = data[key]
    //     }
    // }
}

const getDetail = async (row: Record<string, any>) => {
    // const { id, order_remark } = await orderdetail({
    //     id: row.id
    // })
    // formData.id = id
    // formData.order_remark = order_remark
}


const getType = (str: string, type: string, title:string) => {
    formData.str = str;
    formData.type = type;
    titleStr.value = title || '商品编号'
}


const handleClose = () => {
    emit('close')
}


//提交
const handleSubmit = async () => {
    // await orderRemark(formData)
    await formRef.value?.validate();
    popupRef.value?.close();
    emit('success', formData)
}

defineExpose({
    open,
    getType,
    setFormData,
    getDetail
});

</script>
